package com.SlideMenu.chatlove;


import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.kdstudio.network.URLCachePolicy;
import com.kdstudio.network.URLRequest;
import com.kdstudio.network.URLRequestDelegate;
import com.kdstudio.network.reponse.URLBitmapResponse;
import com.kdstudio.network.reponse.URLJSONReponse;
import com.vg.Adapter.adapter_Friend;
import com.vg.classSample.Friend;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class SampleListFragment_Friend extends ListFragment {
	private Handler mhHandler;
	String url="http://android.dzanchoi.vn/api/images/list.xmob?id=17";
	private SampleAdapter adapter1;
	adapter_Friend adapter;
	ArrayList<Friend> arr=new ArrayList<Friend>();


	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		return inflater.inflate(R.layout.list, null);
	}

	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		  mhHandler=new Handler();
			// adapter1 = new SampleAdapter(getActivity());	
			// setListAdapter(adapter);
			
			URLRequest request=new URLRequest(url, new RequestDelegate());
			//request.setReponse(new URLJSONArrayResponse());
			request.setCachePolicy(URLCachePolicy.MEMCACHE);
			request.send();
	}

	private class SampleItem {
		private String status;
		private String url_img;
		private String name;
		private Bitmap image;
		
		
		
		public String getUrl_img() {
			return url_img;
		}
		public void setUrl_img(String url_img) {
			this.url_img = url_img;
		}
		public SampleItem(String status,String name,String url_img) {
			
			this.status = status; 
			this.url_img = url_img;
			this.name=name;
			
		}
		public String getStatus() {
			return status;
		}
		public void setStatus(String status) {
			this.status = status;
		}
		
		public String getName() {
			return name;
		}
		public void setName(String name) {
			this.name = name;
		}
		public Bitmap getImage() {
			return image;
		}
		public void setImage(Bitmap image) {
			this.image = image;
		}
		
		
		
		public void loadImage() {
			URLRequest request=new URLRequest(getUrl_img(), new RequestDelegate());
			request.setReponse(new URLBitmapResponse());
			request.setCachePolicy(URLCachePolicy.MEMCACHE);
			request.send();
		}
		
		private class RequestDelegate extends URLRequestDelegate
		{
			@Override
			public void onRequestReponse(final URLRequest httpRequest) {
				mhHandler.post(new Runnable() {
					
					@Override
					public void run() {
						// TODO Auto-generated method stub
						URLBitmapResponse reponse=(URLBitmapResponse)httpRequest.getReponse();
						setImage(reponse.bitmap);
						adapter.notifyDataSetChanged();
						
					}
				});
				// TODO Auto-generated method stub
				
			}
			
			//anh hien thi khi timeout
			@Override
			public void onRequestError(URLRequest httpRequest, Throwable error) {
				// TODO Auto-generated method stub
				
			}
			
			//chay tu dau la anh gi khi load xong se thay doi
			@Override
			public void onRequestStart(URLRequest httpRequest) {
				// TODO Auto-generated method stub
				super.onRequestStart(httpRequest);
			}
		}		
	}
	public class SampleAdapter extends ArrayAdapter<SampleItem> {

		private ImageView icon;
		private Button bt_name;
		private TextView tv_status;

		public SampleAdapter(Context context) {
			super(context, 0);
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_contact, null);
			}
			 icon = (ImageView) convertView.findViewById(R.id.contact_img_user_1);
			 if(getItem(position).getImage() != null)
			 {
				icon.setImageBitmap(getItem(position).getImage());
			 }
			 else
			 {
				 icon.setImageResource(R.drawable.ic_launcher);
				 getItem(position).loadImage();
			 }
			bt_name=(Button)convertView.findViewById(R.id.contact_tv_name_1);
			tv_status = (TextView) convertView.findViewById(R.id.contact_tv_status_1);
			
			
			bt_name.setText(getItem(position).name);
			tv_status.setText(getItem(position).status);

			return convertView;
		}

	}
	
	private class RequestDelegate extends URLRequestDelegate
	{
		@Override
		public void onRequestReponse(final URLRequest httpRequest) {
			mhHandler.post(new Runnable() {
				@Override
				public void run() {
					// TODO Auto-generated method stub
					//URLBitmapResponse reponse=(URLBitmapResponse)httpRequest.getReponse();
					//img.setImageBitmap(reponse.bitmap);
					
					URLJSONReponse response=(URLJSONReponse)httpRequest.getReponse();		
					try {
						JSONArray jarr=response.jsonObj.getJSONArray("data");
					for (int i = 0; i < jarr.length(); i++) {
						JSONObject obj=jarr.getJSONObject(i);
						String name_test=obj.getString("name");
						String Status_test=obj.getString("body");	
						String img=obj.getString("images");
						Friend mFriend=new Friend(img,name_test);
						arr.add(mFriend);
						adapter=new adapter_Friend(getActivity(), arr);
					}
					setListAdapter(adapter);
					adapter.notifyDataSetChanged();
					//tv.setText(response.jsonObj.toString());
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					
				}
			});
			// TODO Auto-generated method stub
			
		}
		
		//anh hien thi khi timeout
		@Override
		public void onRequestError(URLRequest httpRequest, Throwable error) {
			// TODO Auto-generated method stub
			
		}
		
		//chay tu dau la anh gi khi load xong se thay doi
		@Override
		public void onRequestStart(URLRequest httpRequest) {
			// TODO Auto-generated method stub
			super.onRequestStart(httpRequest);
		}
	}
}
